//
//  SelectTeacherViewController.m
//  HTP
//
//  Created by admin on 15/11/1.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "SelectTeacherViewController.h"
#import "ScreeningViewController.h"
#import "TeacherDetailViewController.h"
#import "TeacherCell.h"

#import "TeacherViewModel.h"
@interface SelectTeacherViewController ()<BaseTableDelgate,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain) NSDictionary    *titleDic;

@property (nonatomic,retain) NSDictionary    *subDic;
@property (nonatomic,strong) BaseTableView   *teactherTable;

@property (nonatomic,retain) NSArray         *dataArray;
//@property (nonatomic,str)
@end

@implementation SelectTeacherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _initNavSource];
    [self _initView];
    _dataArray = [NSArray array];
    [self getDataFirst];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self _initNav];
}
//TODO: UI 绘制
-(void)_initView{
    [self.view addSubview:self.teactherTable];
}
//TODO:绘制导航栏
-(void)_initNavSource{
    _titleDic = @{
                  @(0):@"语文老师",
                  @(1):@"数学老师",
                  @(2):@"英语老师",
                  @(3):@"科学老师",
                  @(4):@"物理老师",
                  @(5):@"化学老师",
                  @(6):@"文综老师",
                  @(7):@"棋音",
                  @(8):@"体育老师",
                  @(9):@"其他"
                  };
    _subDic = @{
                @(0):@"语文",
                @(1):@"数学",
                @(2):@"英语",
                @(3):@"科学",
                @(4):@"物理",
                @(5):@"化学",
                @(6):@"文综",
                @(7):@"棋音",
                @(8):@"体育",
                @(9):@"其他"
                };
}
-(void)_initNav{
//    [self addRightButtonWithTitle:@"刷选" withAction:@selector(selectTypeTeacher)];
    [self addCustomRightWithAction:@selector(selectTypeTeacher) title:@"筛选" normalImg:nil selectImg:nil];
    [self addCustomTitleWithTitle:_titleDic[@(_titleIndex)]];
    [self addCustomBackWithAction:@selector(backSubVc) normalImg:SYSTEMBACKIMAGE selectImg:SYSTEMBACKIMAGE withPadding:5];
}
//TODO: 重写get方法
-(BaseTableView *)teactherTable{
    if (!_teactherTable) {
        _teactherTable                  = [[BaseTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt - NavHe) style:UITableViewStylePlain withHeadRefresh:NO andFoodRefresh:YES];
        _teactherTable.dataSource       = self;
        _teactherTable.delegate         = self;
        _teactherTable.separatorStyle   = UITableViewCellSeparatorStyleNone;
        _teactherTable.backgroundColor  = [UIColor clearColor];
    }
    return _teactherTable;
}
//TODO: table datasource delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ScreenWidth  - 30 - 20;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10.0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = UIView.new;
    [view setFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = UIView.new;
    [view setFrame:CGRectMake(0, 0, ScreenWidth, 10)];
    [view setBackgroundColor:[UIColor clearColor]];
    return view;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indentifity = @"teacherCell";
    TeacherCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifity];
    if (!cell) {
        cell = [[TeacherCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifity];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    [cell setCellData:[_dataArray objectAtIndex:indexPath.section]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (![USERCONTENT isLoginUser]) {
        [[NSNotificationCenter defaultCenter] postNotificationName:GETUSERLOGIN object:nil];
        return;
    }
    TeacherModel *teacher = (TeacherModel *)[_dataArray objectAtIndex:indexPath.section];
    TeacherDetailViewController *teacherDetail = [[TeacherDetailViewController alloc] init];
    teacherDetail.teacherId = teacher.tchId;
    [self.navigationController pushViewController:teacherDetail animated:YES];
}
//TODO: baseTable Delegate

-(void)footRefresh{
    [self getTeachers:[NSString stringWithFormat:@"%ld",(long)_dataArray.count] andLength:@"10"];
}
//TODO: ACTION
/**
 *  yunhe ---筛选教师条件
 */
-(void)selectTypeTeacher{
    ScreeningViewController *screenVc = [[ScreeningViewController alloc] init];
    screenVc.courseName = _subDic[@(_titleIndex)];
    [self.navigationController pushViewController:screenVc animated:YES];
}
-(void)backSubVc{
    [self.navigationController popViewControllerAnimated:YES];
}
//TODO:数据交互
/**
 *  获取教师列表数据
 */
-(void)getDataFirst{
    TeacherViewModel *teacher = [[TeacherViewModel alloc] init];
    [teacher setBlockWithReturnBlock:^(id returnValue) {
        [self dismissSVProgress];
        _dataArray = [TeacherModel mj_objectArrayWithKeyValuesArray:returnValue[INTERFACE_INFO]];
        [self.teactherTable reloadData];
        
    } WithErrorBlock:^(id errorCode) {
        [self showSVProgressFailed:@"调用接口失败"];
    } WithFailureBlock:^{
        [self showSVProgressFailed:@"网络错误"];
    }];
    [self showSvprogress:@"获取教师列表"];
    [teacher getTeacherBySubject:_subDic[@(_titleIndex)]andStart:@"0" withLength:@"10"];
}
-(void)getTeachers:(NSString *)start andLength:(NSString *)length{
    TeacherViewModel *teacher = [[TeacherViewModel alloc] init];
    [teacher setBlockWithReturnBlock:^(id returnValue) {
        [self dismissSVProgress];
        NSMutableArray *dataarrays = [NSMutableArray arrayWithArray:_dataArray];
        [dataarrays addObjectsFromArray:[TeacherModel mj_objectArrayWithKeyValuesArray:returnValue[INTERFACE_INFO]]];
        _dataArray = [NSArray arrayWithArray:dataarrays];
        [self.teactherTable reloadData];
    } WithErrorBlock:^(id errorCode) {
        [self showSVProgressFailed:@"调用接口失败"];
    } WithFailureBlock:^{
        [self showSVProgressFailed:@"网络错误"];
    }];
    [self showSvprogress:@"获取教师列表"];
    [teacher getTeacherBySubject:_subDic[@(_titleIndex)]andStart:start withLength:length];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
